package tothello;


/**
 * Awesome Code from Qu huanwen.
 * You can tell 250 lines of codes less comparing with my code.
 * @author Qu huanwen
 *
 */
public class HuanwenTothello {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		HuanwenTothello game = new HuanwenTothello();
		String[] redPieces = new String[] { "C2", "C3", "C4", "C5", "D4", "E4",
				"F2", "F3", "F4", "F5", "G6" };
		String[] blackPieces = new String[] { "B1", "E1", "G1", "C6", "H7",
				"G4" };
		String whoseTurn = "Black";
		game.bestMove(redPieces, blackPieces, whoseTurn);

	}

	public int bestMove(String[] redPieces, String[] blackPieces,
			String whoseTurn) {
		try {
			char turn;
			int bestRow = -1, bestCol = -1;
			char[][] bestBoard = null;
			if ("Red".equals(whoseTurn)) {
				turn = 'R';
			} else if ("Black".equals(whoseTurn)) {
				turn = 'B';
			} else {
				throw new Exception("Invalid turn.");
			}
			char[][] board = initBorad(redPieces, blackPieces);
			printBoard(board);
			int max = 0;
			for (int i = 0; i < 8; i++) {
				for (int j = 0; j < 8; j++) {
					if (board[i][j] == ' ') {
						char[][] board2 = copyBoard(board);
						board2[i][j] = turn;

						while (changeColor(board2, turn))
							;
						int count = countPeices(board2, turn);
						if (count > max) {
							max = count;
							bestRow = i;
							bestCol = j;
							bestBoard = board2;
						}
					}
				}
			}

			System.out.println("Best: " + bestRow + ", " + bestCol);
			System.out.println("Pieces: " + max);
			printBoard(bestBoard);
			return max;
		} catch (Exception e) {
			System.out.println(e.getMessage());
			return 0;
		}

	}

	private void printBoard(char[][] board) {
		System.out.print("  ");
		for (int i = 0; i < 8; i++) {
			char c = (char) ('A' + i);
			System.out.print(" " + c);
		}
		System.out.println("");
		for (int i = 0; i < 8; i++) {
			System.out.print((i + 1) + " ");
			for (int j = 0; j < 8; j++) {
				System.out.print(" " + board[i][j]);
			}
			System.out.println("");
		}

	}

	private char[][] copyBoard(char[][] board) {
		char[][] board2 = new char[8][8];
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				board2[i][j] = board[i][j];
			}
		}
		return board2;
	}

	private char[][] initBorad(String[] redPieces, String[] blackPieces)
			throws Exception {
		char[][] board = new char[8][8];
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				board[i][j] = ' ';
			}
		}
		initPieces(board, redPieces, 'R');
		initPieces(board, blackPieces, 'B');

		int reds = redPieces.length;
		int blacks = blackPieces.length;
		if (reds > 50)
			throw new Exception("Too many red pieces.");
		if (blacks > 50)
			throw new Exception("Too many blacks pieces.");
		if (reds + blacks > 63) {
			throw new Exception("No more space to move.");
		}

		if (changeColor(board, 'R') || changeColor(board, 'B')) {
			throw new Exception("Bad state.");
		}

		return board;
	}

	private boolean changeColor(char[][] board, char turn) {
		boolean changed = false;
		for (int n = 0; n < 8; n++) {
			changed = changeColorInLine(board, turn, n, 0, 0, 1) || changed;
			changed = changeColorInLine(board, turn, 0, n, 1, 0) || changed;
			changed = changeColorInLine(board, turn, n, 0, 1, 1) || changed;
			changed = changeColorInLine(board, turn, 0, n, 1, 1) || changed;
			changed = changeColorInLine(board, turn, 0, n, 1, -1) || changed;
			changed = changeColorInLine(board, turn, n, 7, 1, -1) || changed;
		}
		return changed;
	}

	private boolean changeColorInLine(char[][] board, char turn, int row,
			int col, int delta_row, int delta_col) {
		boolean started = false;
		boolean counted = false;
		int count = 0;

		boolean changed = false;

		while (row < 8 && col < 8 && row >= 0 && col >= 0) {
			char c = board[row][col];
			if (c == ' ') {
				started = false;
				counted = false;
				count = 0;
			} else if (c == turn && counted == true) {
				int row1 = row;
				int col1 = col;
				while (count > 0) {
					row1 -= delta_row;
					col1 -= delta_col;
					board[row1][col1] = turn;
					changed = true;
					count--;
				}

				started = false;
				counted = false;
			} else if (c != turn && started == true) {
				counted = true;
				count++;
			} else if (c == turn && started == false) {
				started = true;
			}
			row += delta_row;
			col += delta_col;
		}

		return changed;
	}

	private int countPeices(char board[][], char c) {
		int count = 0;
		for (int i = 0; i < 8; i++) {
			for (int j = 0; j < 8; j++) {
				if (board[i][j] == c)
					count++;
			}
		}
		return count;
	}

	private void initPieces(char board[][], String[] pieces, char c)
			throws Exception {
		for (String piece : pieces) {
			int[] p = getPos(piece);
			int col = p[0];
			int row = p[1];
			if (board[row][col] != ' ') {
				throw new Exception("Duplicated peices on " + piece);
			}
			board[row][col] = c;
		}
	}

	private int[] getPos(String p) throws Exception {
		if (p.length() != 2)
			throw new Exception("Bad position:" + p);
		int col = p.charAt(0) - 'A';
		int row = p.charAt(1) - '1';
		if (col < 0 || col > 7 || row < 0 || row > 7)
			throw new Exception("Bad position:" + p);
		return new int[] { col, row };
	}
}